<?php
class animus_home_annonce extends Model{
	function animus_home_annonce(){
		parent::Model();
		$this->load->database();
	}
	
	function get_all_annonces($per_page, $off_set){
		$this->db->select('*');
		$this->db->from('animus_annonce');
		$this->db->where('active', '1');
		$this->db->limit($per_page, $off_set);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_annonces_by_level($level = NULL, $per_page, $off_set){
		$this->db->select('*');
		$this->db->from('animus_annonce');
		$this->db->where('level', $level);
		$this->db->where('active', '1');
		$this->db->limit($per_page, $off_set);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function count_annonce($level = NULL){
		if($level == NULL){
			$query = $this->db->count_all_results('animus_annonce');	
		}elseif ($level != NULL){
			$this->db->like('level', $level);
			$this->db->from('animus_annonce');
			$query = $this->db->count_all_results();
		}
		return $query;	
	}
	
	function get_premium_annonce_sidebar(){
            $this->db->select('animus_annonce.id,animus_annonce.title, animus_annonce.posted_by, animus_annonce.description, animus_annonce.images,lehu_users.id AS userid, lehu_users.username AS username');
            $this->db->from('animus_annonce,lehu_users');
            $this->db->where('animus_annonce.posted_by = lehu_users.id');
            $this->db->where('animus_annonce.active', '1');
            $this->db->where('animus_annonce.level = 2');
            $this->db->where('animus_annonce.date_expried >'.  time());
            $this->db->order_by('RAND()');
            $this->db->limit(4);
            $query = $this->db->get();
            return $query->result_array();
	}

        function get_alaune_annonce_header(){
            $this->db->select('animus_annonce.id,animus_annonce.title, animus_annonce.posted_by, animus_annonce.description, animus_annonce.images,lehu_users.id AS userid, lehu_users.username AS username');
            $this->db->from('animus_annonce,lehu_users');
            $this->db->where('animus_annonce.posted_by = lehu_users.id');
            $this->db->where('animus_annonce.active', '1');
            $this->db->where('animus_annonce.level = 3');
            $this->db->where('animus_annonce.date_expried >'.  time());
            $this->db->order_by('RAND()');
            $this->db->limit(4);
            $query = $this->db->get();
            return $query->result_array();
	}
	
	function get_premium_annonces(){
		$per_page = $this->uri->segment(3) != '' ? $this->uri->segment(3) : 5;
		$off_set = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 0;
		$config['base_url'] = base_url() . 'index.php/animus_home/list_premium_annonces/' . $per_page . '/';
		$config['per_page'] = 5;
		$config['uri_segment'] = 4;
		$gratuitement_level = 2;
		$config['total_rows'] = $this->Animus_home_annonce->count_annonce($gratuitement_level);
		$this->_data['count_all_annonce'] = $config['total_rows'];
		if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }
        $result = $this->Animus_home_annonce->get_annonces_by_level($gratuitement_level,$per_page, $off_set);
		$this->_data['annonces'] =  $result;
		$this->_data['per_page'] =  $per_page;
        $this->_data['off_set']  =  $off_set;
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
	 	//$this->display_view('home/annonce/list_premium_annonces');
	 
	}
	
	function get_annonce_details($id){
		$this->db->select('animus_annonce.id, animus_annonce.title, animus_annonce.images, animus_annonce.content, 
		                   animus_annonce.date_posted, animus_annonce.date_expried, animus_annonce.posted_by, lehu_users.username');   
		$this->db->from('animus_annonce');
		$this->db->join('lehu_users', 'animus_annonce.posted_by = lehu_users.id');
		$this->db->where('animus_annonce.id', $id);
		$query = $this->db->get();
		return $query;	
	}
	
	function list_home_annonces($per_page, $off_set){
		$this->db->select();
		$this->db->from('animus_annonce');
		$this->db->where('active', '1');
		$this->db->where('date_expried >=', time());
		$this->db->order_by('level','desc');
		$this->db->order_by('date_posted','desc');
		$this->db->limit($per_page, $off_set);
		$query = $this->db->get();
		return $query->result_array();
	}
}